import { useCallback, useEffect, useRef } from "react";
export default function useDebounceEffect(
  fn: React.EffectCallback,
  deps: React.DependencyList | undefined,
  delay = 500
) {
  const timer = useRef<NodeJS.Timeout | null>(null);
  const debounceFn = () => {
    if (timer.current !== null) {
      clearTimeout(timer.current);
    }
    timer.current = setTimeout(() => {
      fn();
    }, delay);
    return () => {
      console.log("执行了清理!");
      if (timer.current) {
        clearTimeout(timer.current);
      }
    };
  };
  useEffect(debounceFn, deps);
}
